package cn.hj.pet.service.impl;

import cn.hj.basic.service.impl.BaseServiceImpl;
import cn.hj.basic.util.CodeGenerateUtils;
import cn.hj.basic.util.DistanceUtil;
import cn.hj.basic.util.PageList;
import cn.hj.basic.util.Point;
import cn.hj.order.domain.PetAcquisitionOrder;
import cn.hj.order.mapper.PetAcquisitionOrderMapper;
import cn.hj.org.domain.Employee;
import cn.hj.org.domain.Shop;
import cn.hj.org.mapper.EmployeeMapper;
import cn.hj.org.mapper.ShopMapper;
import cn.hj.pet.domain.Pet;
import cn.hj.pet.domain.SearchMasterMsg;
import cn.hj.pet.mapper.PetMapper;
import cn.hj.pet.mapper.SearchMasterMsgMapper;
import cn.hj.pet.query.SearchMasterMsgQuery;
import cn.hj.pet.service.IPetService;
import cn.hj.pet.service.ISearchMasterMsgService;
import cn.hj.user.domain.Logininfo;
import cn.hj.user.domain.User;
import cn.hj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService PetService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;//宠物收购订单

    /**
     * 发布寻主信息
     * @param searchMasterMsg
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg,Logininfo logininfo) {
        //获取到当前登录人信息,绑定登录人信息
        User user  = userMapper.getByLogInfoId(logininfo.getId());
        //设置登录人id
        searchMasterMsg.setUser_id(user.getId());
        //绑定最近店铺信息
        //1获取地址经纬度
        Point LoginPoint = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //查询所有店铺地址信息
        List<Shop> shops = shopMapper.selectAll();
        //查询到距离最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(LoginPoint, shops);
        //设置店铺id
        searchMasterMsg.setShop_id(nearestShop.getId());
        //最后保存数据
        searchMasterMsgMapper.insert(searchMasterMsg);
    }

    /**
     * 查询用户自己的寻主信息
     * @param query
     * @param currentLogininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user  = userMapper.getByLogInfoId(logininfo.getId());
        query.setUser_id(user.getId());
        return super.selectData(query);
    }


    /**
     * 查询待处理信息
     * @param query
     * @param currentLogininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(0);//待处理信息
        Employee employee  = employeeMapper.getByLogInfoId(logininfo.getId());
        if (employee.getShopId()!=null){
            query.setShop_id(employee.getShopId());//查询某店铺的数据
        }
        return super.selectData(query);
    }


    /**
     * 查询已处理信息
     * @param query
     * @param currentLogininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(1);//已处理信息
        Employee employee  = employeeMapper.getByLogInfoId(logininfo.getId());
        if (employee.getShopId()!=null){
            query.setShop_id(employee.getShopId());//查询某店铺的数据
        }
        return super.selectData(query);
    }

    /**
     * 处理信息
     * @param pet
     * @param logininfo
     */
    @Override
    @Transactional
    public void handle(Pet pet, Logininfo logininfo) {
        //修改处理状态(通过发布人id）
        searchMasterMsgMapper.update2StateById(pet.getSearch_master_msg_id());
        //保存pet和PetDetail
        PetService.add(pet);
        //生成收购订单
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(pet.getSearch_master_msg_id());
        Employee employee  = employeeMapper.getByLogInfoId(logininfo.getId());
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg,employee.getId());
        petAcquisitionOrderMapper.insert(petAcquisitionOrder);

        //@TODO
    }
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long id) {
       PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
       order.setPrice(pet.getCostprice());
       order.setAddress(adopt.getAddress());
       String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
       order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
       order.setUser_id(adopt.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(id);
        return order;
    }
}
